//
//  BBHomeHeaderView.m
//  BlockBrowser
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHomeHeaderView.h"
#import "KLClearTableHeaderView.h"
#import "BBHomeBezierView.h"
#import "BBArticleViewController.h"
@interface  BBHomeHeaderView()<UIGestureRecognizerDelegate>
@property(nonatomic, assign) CGFloat                     searchViewHeight;
@property(nonatomic, assign) CGFloat                     searchViewCenterY;

@property(nonatomic, assign) CGFloat                     menuViewHeight;
@property(nonatomic, assign) CGFloat                     menuViewCenterY;

@property(nonatomic, strong) BBHomeBezierView                     *bezierView;
@property(nonatomic, assign) CGPoint    originPoint;

@property(nonatomic, strong) STLabel    *refreshLable;

@property(nonatomic, assign) BOOL   testAnimation;


@end
@implementation BBHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configSubView];
    }
    return self;
}
#pragma mark --subView
- (void)configSubView{
    // self.backgroundColor = TM_ThemeBackGroundColor;
    __weak typeof(self) weakSelf =  self;
    self.searchView = [[BBHomeSearchVIew alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
    self.searchView.backgroundColor = [BBThemeColorManger manger].themeColor;
    [self.searchView setOnSlectedUserHandle:^(UIButton *sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(homeHeaderVewSelctedSearchWithTitle:)]) {
            [weakSelf.delegate homeHeaderVewSelctedSearchWithTitle:@"用户"];
        }
    }];
    [self.searchView setOnSlectedSearchHandle:^(UIButton *sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(homeHeaderVewSelctedSearchWithTitle:)]) {
            [weakSelf.delegate homeHeaderVewSelctedSearchWithTitle:@"搜索"];
        }
    }];
    [self addSubview:self.searchView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)
                                                     text:@"松开刷新并且进入今日文章"
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerY = self.searchView.height / 2;
    titleLable.alpha = 0;
    self.refreshLable = titleLable;
    [self addSubview:titleLable];
    
    self.bezierView = [[BBHomeBezierView alloc] initWithFrame:self.searchView.bounds];
    self.bezierView.backgroundColor = TM_ThemeBackGroundColor;
    [self insertSubview:self.bezierView  belowSubview:self.searchView];
    
    
    self.menuView = [[BBHomeMenuView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
    self.menuView.top = self.searchView.bottom;
    self.menuView.backgroundColor = [UIColor whiteColor];
    if (BBThemeColorManger.isBlack) {
       self.menuView.backgroundColor = [BBThemeColorManger manger].themeColor;
    }
    
    [self.menuView setOnSlectedMenuHandle:^(NSString *title) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(homeHeaderVewSelctedMenuWithTitle:)]) {
            [weakSelf.delegate homeHeaderVewSelctedMenuWithTitle:title];
        }
    }];
    [self addSubview:self.menuView];
    
    self.searchViewHeight = self.searchView.height;
    
    
    self.menuViewHeight = self.menuView.height;
    self.menuViewCenterY = self.menuView.centerY;
    
    
    
    self.height = self.menuView.bottom;
    
    UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pangesAction:)];
    panGes.delegate = self;
  //  [self addGestureRecognizer:panGes];
}
//相应tableView 结束时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView{
    if (self.refreshLable.alpha > 0.95) {
        self.canShowRefreshAnimation = YES;
        [self forceEndanimationAndWillPush];
        self.testAnimation = YES;
    }else{
        //  self.canShowRefreshAnimation = NO;
    }
    
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    //    if (self.canShowRefreshAnimation) {
    //        [self weakTableAnimationToTop];
    //    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.canShowRefreshAnimation) {
        
        //    [self.weakTableView setContentOffset:CGPointMake(0, self.height) animated:YES];
        
    }
}
- (void)scrollviewDidScroll:(UITableView *)scrollView{
    //    NSInteger test = scrollView.decelerating;
    //    NSLog(@"是否滑动完毕%ld",test);
    return;
    CGFloat scrollY = scrollView.contentOffset.y;
    [self tabbleHeaderWithScrollView:scrollView];
    
    if (scrollY < self.menuView.height && scrollY > 0) {
        [self menuViewAnimationWithScrollView:scrollView];
    }
    if (scrollY > self.menuView.height) {
        [self searchAnimationWithScrollView:scrollView];
    }
    if (scrollY <= 0) {
        //menu 跟着刷新
        self.menuView.top = self.searchView.bottom -  scrollY;
        //贝塞尔
        [self.bezierView updateWithY:(0 - scrollY)];
        [self searchBootomAnimationWithScrollView:scrollView];
    }
}
#pragma mark --Private Method
- (void)tabbleHeaderWithScrollView:(UITableView *)scrollView{
    KLClearTableHeaderView * tableHeader  = (id)scrollView.tableHeaderView ;
    CGFloat scrollY = scrollView.contentOffset.y;
    //0 ~0.4;
    CGFloat maxAlpha = 0.3;
    CGFloat chazhiBili = scrollY / self.height;
    CGFloat alpha = (1-maxAlpha) * chazhiBili;
    
    tableHeader.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
    
}
//往上search view 动画
- (void)searchAnimationWithScrollView:(UITableView *)scrollView{
    CGFloat scrollY = scrollView.contentOffset.y - self.menuViewHeight;
    //1 ~0.98;
    CGFloat maxScale = 0.96;
    CGFloat chazhiBili = scrollY / self.searchView.height;
    CGFloat scale = (1-maxScale) * chazhiBili;
    CGFloat realscale  = 1 - scale;
    self.searchView.transform = CGAffineTransformMakeScale(realscale, realscale);
}
- (void)searchBootomAnimationWithScrollView:(UITableView *)scrollView{
    CGFloat scrollY = ABS(scrollView.contentOffset.y);
    //1 ~0.6;
    CGFloat maxY = 80;
    CGFloat maxalphy = 0.01;
    CGFloat chazhiBili = scrollY / maxY;
    chazhiBili = chazhiBili >1?1:chazhiBili;
    CGFloat alphy = 1-chazhiBili*(1-maxalphy) ;
    self.searchView.alpha = alphy;
    
    self.refreshLable.alpha = 1- alphy;
    
    
}
//菜单动画
- (void)menuViewAnimationWithScrollView:(UITableView *)scrollView{
    CGFloat scrollY = scrollView.contentOffset.y;
    
    //alpha  1 ~   0.6  scale 1~0.8
    CGFloat maxAlpha = 0.1;
    CGFloat maxScale = 0.90;
    CGFloat chazhiBili = scrollY / self.menuView.height;
    
    CGFloat alpha = (1-maxAlpha) * chazhiBili;
    CGFloat realalpha  = 1 - alpha;
    self.menuView.alpha = realalpha;
    
    
    CGFloat scale = (1-maxScale) * chazhiBili;
    CGFloat realscale  = 1 - scale;
    // self.menuView.size = CGSizeMake(realscale * UIScreenWidth, realscale * self.menuViewHeight);
    self.menuView.transform = CGAffineTransformMakeScale(realscale, realscale);
    self.menuView.centerY = self.menuViewCenterY;
    self.menuView.centerX = UIScreenWidth / 2;
    
    
}
#pragma mark --Action Method
- (void)pangesAction:(UIPanGestureRecognizer*)panges{
    CGPoint point = [panges locationInView:self];
    NSLog(@"header手势%0.2f",point.y);
    
    CGFloat realYidong = self.originPoint.y - point.y;
    [self.weakTableView setContentOffset:CGPointMake(0, realYidong * 0.5) animated:NO];
    if (panges.state == UIGestureRecognizerStateEnded || panges.state == UIGestureRecognizerStateCancelled) {
        if (self.weakTableView.contentOffset.y < self.height * 0.3) {
            [self.weakTableView setContentOffset:CGPointZero animated:YES];
            
        }else{
            [self.weakTableView setContentOffset:CGPointMake(0, self.height) animated:YES];
        }
        
        if (self.refreshLable.alpha > 0.95) {
            [self forceEndanimationAndWillPush];
        }
        
    }else{
        
    }
    
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    CGPoint point = [gestureRecognizer locationInView:self];
    self.originPoint = point;
    return YES;
    
}
#pragma mark --Private Method
- (void)forceEndanimationAndWillPush{
    [self.weakTableView setContentInset:UIEdgeInsetsMake(-self.weakTableView.contentOffset.y, 0, 0, 0)];
    self.weakTableView.contentInset = UIEdgeInsetsZero;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.weakTableView setContentOffset:CGPointMake(0, self.height - 64) animated:NO];
        
        
    }completion:^(BOOL finished) {
        [self.bezierView updateWithY:0];
        
        
    }];
}
//滑动到顶部
- (void)weakTableAnimationToTop{
    [self.weakTableView setContentOffset:CGPointMake(0, self.height) animated:YES];
}
@end

